package io.sage.modules.readsenseapi.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.github.pagehelper.PageInfo;
import io.sage.modules.flow.service.FlowCustomerGroupsService;
import io.sage.modules.flow.service.FlowCustomersService;
import io.sage.modules.readsenseapi.entity.BFlowCustomerGroups;
import io.sage.modules.readsenseapi.entity.BFlowCustomers;
import io.sage.modules.readsenseapi.vo.CustomerGroupsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.sage.common.utils.R;

import static java.lang.Long.parseLong;

/**
 *
 *
 */
@RestController
@RequestMapping("flow/flowcustomergroups")
public class FlowCustomerGroupsController {
    @Autowired
    private FlowCustomerGroupsService flowCustomerGroupsService;
    @Autowired
    private FlowCustomersService flowCustomersService;

    /**
     * 列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("flow:flowcustomergroups:list")
    public R list(@RequestParam Map<String, Object> params) {
        //赋值当前页初始值
        Integer pageNum = 1;
        //赋值显示个数
        Integer pageSize = 10;
        //判断接收到的前端数据params是否有值及页数限制是否有值
        if (params != null && params.get("limit") != null) {
            pageSize = java.lang.Integer.parseInt(params.get("limit").toString());
        }
        //判断页数是否有值
        if (params != null && params.get("page") != null) {
            pageNum = java.lang.Integer.parseInt(params.get("page").toString());
        }
        //封装页面数据到map集合传输到service层
        Map<String,Object> map=new HashMap();
        //当前页
        map.put("pageNum",pageNum);
        //个数
        map.put("pageSize",pageSize);
        //调用查询方法并封装到集合内
         map = flowCustomerGroupsService.selectFlowCustomerGroupsList(map);
        PageInfo<BFlowCustomerGroups> pageInfo = new PageInfo<BFlowCustomerGroups>((List<BFlowCustomerGroups>) map.get("list"));// 传入list就可以了

        //设置前端页面所需数据
        //总数据条数
        Double total=Double.parseDouble((String) map.get("total"));
        //当前页面最后一个元素在数据库中的行号
        pageInfo.setEndRow(((List<BFlowCustomerGroups>) map.get("list")).size());
        double num=total/pageSize;
        double num1=Math.ceil(num);
        //判断是否有下一页
        Integer lastPage=(int)num1;
        pageInfo.setHasNextPage(pageNum<lastPage);
        //判断是否是第一页
        pageInfo.setIsFirstPage(pageNum==1);
        //判断是否是最后一页
        pageInfo.setIsLastPage(pageNum==lastPage);
        //设置最后页数数量
        pageInfo.setLastPage(lastPage);
        //设置第一页
        pageInfo.setNavigateFirstPage(1);
        //设置最后一页
        pageInfo.setNavigateLastPage(lastPage);
        //循环得到所有页数
        int[] list=new int[lastPage];
        for (int i=1;i<=lastPage;i++){
            list[i-1]=i;
        }
        //当前页面第一个元素在数据库中的行号
        pageInfo.setStartRow(1);
        //总页数
        pageInfo.setPages(lastPage);
        //所有导航页号
        pageInfo.setNavigatepageNums(list);
        //下一页
        pageInfo.setNextPage(pageNum<lastPage?pageNum+1:lastPage);
        //总记录数
        pageInfo.setTotal(parseLong((String) map.get("total")));
        //每页记录数
        pageInfo.setPageSize(pageSize);
        //当前页
        pageInfo.setPageNum(pageNum);
        return R.ok().put("bFlowCustomerGroups", pageInfo);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
//    @RequiresPermissions("flow:flowcustomergroups:info")
    public R info(@PathVariable("id") String id) {
        BFlowCustomerGroups flowCustomerGroups = flowCustomerGroupsService.selectByPrimaryKey(id);
        return R.ok().put("flowCustomerGroups", flowCustomerGroups);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
//    @RequiresPermissions("flow:flowcustomergroups:save")
    public R save(@RequestBody CustomerGroupsVo flowCustomerGroups) {
        int insert = flowCustomerGroupsService.insert(flowCustomerGroups);
        if (insert > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
//    @RequiresPermissions("flow:flowcustomergroups:update")
    public R update(@RequestBody CustomerGroupsVo flowCustomerGroups) {
        int upd = flowCustomerGroupsService.updateByPrimaryKeySelective(flowCustomerGroups);
        if (upd > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
//    @RequiresPermissions("flow:flowcustomergroups:delete")
    public R delete(@RequestBody String[] ids) {
        int del = flowCustomerGroupsService.deleteBatchIds(ids);
        if (del > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 详情页展示
     */
    @RequestMapping("/customer")
    public R customer(@RequestBody String id) {
        List<BFlowCustomers> bFlowCustomers = flowCustomerGroupsService.selectByBFlowCustomerGroups(id);
        return R.ok().put("bFlowCustomers", bFlowCustomers);
    }
}
